<?php

namespace Model;

class Car extends \Lib\Model {
	public function updateFromAutohome() {
		$sUrl = 'http://car.autohome.com.cn/zhaoche/pinpai/';
		if (($sHtml = @file_get_contents ( $sUrl )) !== false) {
			preg_match_all ( '/<div class="grade_js_top32"  ><a href="(http:\/\/.+?\.html)" target="_blank"><img src="(.+?)" style="border:0px;" \/><\/a>/s', $sHtml, $aMatches );
			$aData = array ();
			foreach ( ( array ) $aMatches [1] as $k => $sUrl ) {
				$aData [] = array (
						$sUrl,
						$aMatches [2] [$k] 
				);
			}
			file_put_contents ( ROOT . '/cache/car.cache', json_encode ( $aData ) );
			$this->updateFromCache ();
			return true;
		} else {
			return false;
		}
	}
	public function updateFromCache() {
		$sData = @file_get_contents ( ROOT . '/cache/car.cache' );
		$aData = json_decode ( $sData, true );
		list ( $sUrl, $sLogoUrl ) = $aData [0];
		if (($sHtml = @file_get_contents ( $sUrl )) != '') {
			if (preg_match ( '/<div class="cront">(.+?)<\/div><\/div>\s+<script type="text\/javascript">/s', $sHtml, $aMatch )) {
				$sHtml = $aMatch [0];
				preg_match ( '/<h1><a href=".+?">(.+?)<\/a><\/h1>/', $sHtml, $aMatch );
				preg_match_all ( '/<a href="\/price\/series.+?" title=".+?">(.+?)<\/a>/s', $sHtml, $aMatches );
			} elseif (preg_match ( '/<div class="s_menu">(.+?)<\/b><\/div>/s', $sHtml, $aMatch )) {
				$sHtml = $aMatch [0];
				preg_match ( '/<a style="text-decoration:none;font-weight:bold;".+?>(.+?)<\/a>/', $sHtml, $aMatch );
				preg_match_all ( '/<a title=".+?" href=".+?" class="" >(.+?)\(\d+/s', $sHtml, $aMatches );
			} elseif (preg_match ( '/<div class="patit">(.+?)<\/table><\/div>\s+<script language="javascript" type="text\/javascript">/s', $sHtml, $aMatch )) {
				$sHtml = $aMatch [0];
				preg_match ( '/<h2><a href=".+?" >(.+?)<\/a><\/h2>/', $sHtml, $aMatch );
				preg_match_all ( '/<h3><a href=".+?" target="_self">(.+?)<\/a><\/h3>/s', $sHtml, $aMatches );
			}
			$sBrandName = iconv ( 'gbk', 'utf-8', $aMatch [1] );
			$aBrandInfo = $this->getBrandInfo ( $sBrandName, 'name' );
			if ($aBrandInfo == false) {
				$iBrandId = $this->addBrand ( $sBrandName );
			} else {
				$iBrandId = $aBrandInfo ['id'];
			}
			$sLogoFile = ROOT . '/images/brand/' . $iBrandId . '.jpg';
			if (! is_file ( $sLogoFile )) {
				@copy ( $sLogoUrl, $sLogoFile );
			}
			foreach ( ( array ) $aMatches [1] as $sModelName ) {
				$sModelName = iconv ( 'gbk', 'utf-8', $sModelName );
				$aModelInfo = $this->getModelInfo ( $sModelName, 'name', $iBrandId );
				if (! $aModelInfo) {
					$this->addMdoel ( $iBrandId, $sModelName );
				}
			}
			unset ( $aData [0] );
			sort ( $aData );
			file_put_contents ( ROOT . '/cache/car.cache', json_encode ( $aData ) );
			if ($aData) {
				header ( 'Content-Type: text/html;charset=utf-8' );
				exit ( '<h3>' . str_repeat ( '.', count ( $aData ) ) . '</h3><script>location.href="./?a=car&m=_update"</script>' );
			}
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 获取汽车品牌信息
	 *
	 * @param int $iId        	
	 * @param string $sType        	
	 */
	public function getBrandInfo($iId, $sType = 'id') {
		return $this->DB->get ( 'car_brand', array (
				$sType => $iId 
		) );
	}
	public function getModelInfo($iId, $sType = 'id', $iBrandId = 0) {
		if ($sType == 'id') {
			$aWehre = array (
					'id' => $iId 
			);
		} else {
			$aWehre = array (
					'name' => $iId,
					'brand_id' => $iBrandId 
			);
		}
		return $this->DB->get ( 'car_model', $aWehre );
	}
	public function addBrand($sName) {
		return $this->DB->insert ( 'car_brand', array (
				'name' => $sName 
		) );
	}
	public function addMdoel($iBrandId, $sName) {
		$aSearch = array (
				'/\(停售\)/',
				'/\(进口\)/' 
		);
		$sName = preg_replace ( $aSearch, '', $sName );
		return $this->DB->insert ( 'car_model', array (
				'brand_id' => $iBrandId,
				'name' => trim ( $sName ) 
		) );
	}
	public function lsBrand() {
		$aList = $this->DB->ls ( array (
				'table' => 'car_brand',
				'format' => array (
						'logo' => array (
								'model' => 'Car',
								'method' => 'getBrandLogoUrl',
								'param' => array (
										'$this->id' 
								) 
						) 
				) 
		) );
		return $this->ArrayExt->sort ( $aList ['items'], 'name' );
	}
	public function lsModel($iBrandId) {
		$aList = $this->DB->ls ( array (
				'table' => 'car_model',
				'where' => array (
						'brand_id' => $iBrandId 
				) 
		) );
		return $this->ArrayExt->sort ( $aList ['items'], 'name' );
	}
	public function getBrandLogoUrl($iId) {
		return $this->Config->get ( 'base.site_url' ) . '/images/brand/' . $iId . '.jpg';
	}
}